<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			div {
				height: 20px;
				margin: 1px;
			}
		</style>
	</head>
	<body>
		<input type="button" name="" value="点击一下" id="btn">
		<div id="div0"></div>
	</body>
	<script>
		var allDiv = document.getElementsByTagName('div');
		var obtn = document.getElementById('btn');
		var a = 0;
		obtn.onclick = function() {
			a++;
			console.log(a)
			if (a <= 1) {
				for (var j = 0; j < 20; j++) {
					document.getElementById('div0').innerHTML += "<div></div>"
				}
			}
			if (a % 2 == 0) {
				for (var i = 0; i < allDiv.length; i++) {
					if (i % 2 == 0) {
						allDiv[i].style.background = 'yellow'
					} else {
						allDiv[i].style.background = 'red'
					}
				}
			} else {
				for (var i = 0; i < allDiv.length; i++) {
					if (i % 2 == 1) {
						allDiv[i].style.background = 'yellow'
					} else {
						allDiv[i].style.background = 'red'
					}
				}
			}
		}
	</script>
</html>